package HierarchyPackage.ProductHierarchy;

import HierarchyPackage.*;
import java.util.Arrays;

/**
 * Classification class belonging to a ProductHierarchy.
 * This class can not be instantiated - only returned from a ProductHierarchy
 * (i.e. only a IntersectionHierarchy can instantiate it).
 *
 * @author Martin Haulrich
 */
public final class ProductHClass implements HClass {

    /**
     * The classes that makes up this intersecting class
     */
    final HClass[] classes;

    /**
     * The IntersectionHierarchyTT this class belongs to.
     */
    final ProductHierarchy hier;

    int hash;


    /** Constructor
     * @param hier The ProductHierarchy this class belongs to.
     * @param classes The classes thats makes up this intersection classification
     */
    public ProductHClass(ProductHierarchy hier, HClass[] classes) {
        this.hier = hier;
        // Make sure that the right information is saved in case HClass[] is overwritten

        // JDK 1.6
        this.classes = Arrays.copyOf(classes, classes.length);
        hash = Arrays.hashCode(classes);

    // JDK 1.5
    //this.classes = new HClass[classes.length];
    //System.arraycopy(classes,0,this.classes,0,classes.length);


    }


    @Override
    public String getName() {
        return hier.getName(this);
    }


    /**
     * Returns the classes that identifies this class
     * @return the classes that identifies this class.
     */
    public HClass[] getID() {
        return classes;
    }


    @Override
    public Hierarchy belongsTo() {
        return hier;
    }


    @Override
    public int hashCode() {
        return hash;
        //return Arrays.hashCode(classes);
    }


    @Override
    public boolean equals(Object o) {
        if (o instanceof ProductHClass) {
            return Arrays.equals(((ProductHClass) o).getID(), classes);
        }
        return false;
    }
}
